from flask import Flask, render_template, jsonify, send_from_directory
import pandas as pd
import os

app = Flask(__name__, static_folder='static')

# 读取Excel数据
def load_data():
    file_path = r'D:\2025工作\AI大模型\3-Cursor编程-从入门到精通\3-Cursor编程-从入门到精通\【完成参考】dashboard_epidemic\test2\香港各区疫情数据_20250322.xlsx'
    return pd.read_excel(file_path)

# 首页路由
@app.route('/')
def index():
    return render_template('index.html')

# 大屏路由
@app.route('/dashboard')
def dashboard():
    return render_template('dashboard.html')

# 静态文件服务
@app.route('/static/<path:path>')
def serve_static(path):
    return send_from_directory('static', path)

# 数据API接口
@app.route('/api/data')
def get_data():
    df = load_data()
    # 返回各地区累计确诊数据
    result = {
        'regions': df['地区名称'].unique().tolist(),
        'data': df.groupby('地区名称')['累计确诊'].max().to_dict(),
        'updateTime': pd.Timestamp.now().strftime('%Y-%m-%d %H:%M:%S')
    }
    return jsonify(result)

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)